import logging

from global_config import weatherkey
from utils.session import session
from utils.decorators.cache import cache
from . import api


@api.route("/weather", methods=["GET"])
@cache(set(), 60)
def handle_weather():
    weather_info = {}
    try:
        weather = session.get(
            url="https://restapi.amap.com/v3/weather/weatherInfo?city=140108&key=" + weatherkey).json()
        if int(weather['status']) == 1:
            weather_info = {
                "type": weather['lives'][0]['weather'],
                "temperature": weather['lives'][0]['temperature'] + '°C'
            }
        return {
            "code": 0,
            "data": {
                "weather": weather_info
            }
        }
    except Exception:
        logging.error("获取天气异常!")
